<?php

class LoginController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{

			return View::make('index');
			
	}	

	public function logincheck(){

		$input=Input::all();
		//var_dump($input);
		//return $input['username'];
		//return $input['password'];
		Auth::Attempt(array(
			'mobile'=>$input['username'],
			'password'=>$input['password']
			),true);
		//var_dump(Auth::check());
		if(Auth::check()==false){
			Auth::Attempt(array(
			 'email'=>$input['username'],
		      'password'=>$input['password']			
				),true);
		//var_dump(Auth::check());
		}

		if (!Auth::check()){
			
			return Response::json(ResponseManager::getError(array(), 00, 'Username or password mismatch.'));
		}else{
			
			 return Response::json(ResponseManager::getResult(Auth::user(), 'SuccessFully Logged In.'));
		}
}
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function store()
	{
		
		$input=Input::all();
		//$input=array(
//			'_id'=>"USR".date('YmdHis'),
//			'UserEmail'=>'ycpatel813@gamil.com',
//			'UserPhone'=>'9033108567',
//			'password'=>'1234',
//			'UserRegistrationDate'=>date('YmdHis')
		//		);
		$validation=Login::validation($input);
		//var_dump($validationchk);exit;
		if($validation->fails()){
			
			return	 $message=$validation->messages();
				
				//return Response::json(ResponseManager::getError(array(), 00, 'validation'));
				//return "hi";
		}else{
			Login::create($input);
			return "Data Added SuccessFully";
			//return $data['name'];
		}

	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function create()
	{

	//	$user=DB::table('users')->get();
		//Post::orderBy('id', 'desc')->get();

		//$user->_id=DB::table('users')->orderBy('_id', 'desc')->first();
		//$user->_id = $user->_id + 1 ;
	//print_r($user['_id']);
	//exit;		  
			$input=array(
				'UserId'=>'3',
				'name'=>'yaad1',
				'password'=>'yaad1',
				'email'=>'1234',
				'phone'=>'9033108567'
				);


			   $user = User::create($input);
			   $user->save();
			   return "You have SuccessFully Added that Data";
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{

	//$input=Login::ShowData($id);
			// $input = User::where('_id', $id)
   //          	   ->take(1)
   //          	   ->get();
		
      $input = Login::where('_id', '=', $id) ->first();
		// $arr = array();
		// 	foreach ($input->toArray() as $key => $value) {
		// 			$arr[$key] = $value['_id'];
		// 			}
			if($input==null){

				return "No Data Available";
					
			}else{
				return $input;
			}

			//return $input['_id'];

         // return Response::json(array(
         //    'error' => false,
         //    'users' => $input->toArray()),
         //    200);
}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
	
			$data = Login::where('_id', '=', $id) ->first();
			//$data->_id=$id;
			$lenth=count($data);
		if($lenth=="1"){	
			$data->name = 'Professor X';
			$data->save();
			return $data;
			//return $data['name'];
		}else
		{
			return "No Record Found";
		}
	

	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
	
	$data = Login::where('_id', '=', $id) ->first();
    $input=Input::all();
//	 	$input=array(
//			'UserId'=>$id,
//			'UserEmail'=>'ycpatel813@gamil.com',
//			'UserPhone'=>'90331085678',
//			'password'=>'1dsfsdfd234',
//			'UserRegistrationDate'=>date('YmdHis')
//				);


	 	$lenth=count($data);
		if($lenth=="1"){	
				
				  $User = DB::table('users')
			                ->where('_id', $id)
            			    ->update($input);
          		return "Data Updated Successfully";  			    
		}else
		{
			return "No Record Found";
		}
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//$user=Login::find($id);
		 $user = DB::table('users')
			                ->where('_id', $id);

		if($user){
			$user->delete();
			return   $message = 'Deleted Successfully.';
		}else{
			return  $message = 'Ooopss...! Not deleted.';
		}
	}


    /**
     * Logout for users module
     *
     * @return Response
     */
    public function logout() {

        Auth::logout();
        Session::flush();

        return Redirect::to('/');
    }

}
